package com.ruoyi.web.wx.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.upload.QiniuUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.CustomId;
import com.ruoyi.system.domain.wx.WxIcon;
import com.ruoyi.system.service.wx.IWxIconService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * icon图标Controller
 *
 * @author mic
 * @date 2023-09-12
 */
@RestController
@RequestMapping("/wxIcon")
public class WxIconController extends BaseController {
    @Autowired
    private IWxIconService wxIconService;

    /**
     * 查询icon图标列表
     */
    @PreAuthorize("@ss.hasPermi('wxIcon:wxIcon:list')")
    @GetMapping("/list")
    public TableDataInfo list(WxIcon wxIcon) {
        startPage();
        List<WxIcon> list = wxIconService.selectwxIconList(wxIcon);
        return getDataTable(list);
    }

    /**
     * 导出icon图标列表
     */
    @PreAuthorize("@ss.hasPermi('wxIcon:wxIcon:export')")
    @Log(title = "icon图标", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WxIcon wxIcon) {
        List<WxIcon> list = wxIconService.selectwxIconList(wxIcon);
        ExcelUtil<WxIcon> util = new ExcelUtil<WxIcon>(WxIcon.class);
        util.exportExcel(response, list, "icon图标数据");
    }

    /**
     * 获取icon图标详细信息
     */
    @PreAuthorize("@ss.hasPermi('wxIcon:wxIcon:query')")
    @GetMapping(value = "/{iconId}")
    public AjaxResult getInfo(@PathVariable("iconId") String iconId) {
        return success(wxIconService.selectwxIconByIconId(iconId));
    }

    /**
     * 新增icon图标
     */
    @PreAuthorize("@ss.hasPermi('wxIcon:wxIcon:add')")
    @Log(title = "icon图标", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WxIcon wxIcon) {
        if (StringUtils.isBlank(wxIcon.getIconId())) {
            wxIcon.setIconId("i" + CustomId.timestamp());
        }
        if (wxIcon.getCreateTime() ==null){
            wxIcon.setCreateTime(new Date());
        }
        return toAjax(wxIconService.insertwxIcon(wxIcon));
    }

    /**
     * 修改icon图标
     */
    @PreAuthorize("@ss.hasPermi('wxIcon:wxIcon:edit')")
    @Log(title = "icon图标", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WxIcon wxIcon) {
        return toAjax(wxIconService.updatewxIcon(wxIcon));
    }

    /**
     * 删除icon图标
     */
    @PreAuthorize("@ss.hasPermi('wxIcon:wxIcon:remove')")
    @Log(title = "icon图标", businessType = BusinessType.DELETE)
    @DeleteMapping("/{iconIds}")
    public AjaxResult remove(@PathVariable String[] iconIds) {
        return toAjax(wxIconService.deletewxIconByIconIds(iconIds));
    }

    /**
     * 图片上传
     */
    @PostMapping("/upload")
    public AjaxResult upload(MultipartFile file) {
        if (file.isEmpty()) {
            return AjaxResult.error("没有接收到实体!");
        }
        long size = file.getSize();

        String fileName = file.getOriginalFilename();
        String extName = "." + fileName.substring(fileName.lastIndexOf(".") + 1);
        fileName = "mh/icon/" + CustomId.timestamp() + extName;
        try {
            boolean upload = QiniuUtils.upload(file.getInputStream(), fileName);
            if (upload) {
                JSONObject json = new JSONObject();
                json.put("url", QiniuUtils.DOMAIN + "/" + fileName);
                json.put("size", size);
                return AjaxResult.success("上传成功!", json);
            } else {
                return AjaxResult.error("上传失败!");
            }
        } catch (IOException ioException) {
            return AjaxResult.error("上传失败!");
        }
    }
}
